<template>
	<view class="container">
		
		<!--tabbar-->
		<view class="tui-tabbar">
			<block v-for="(item,index) in tabbar" :key="index">
				<view class="tui-tabbar-item" :class="[tabCur==index?'tui-item-active':'']" :data-index="index" @tap="tabbarSwitch">
					<view :class="[index==0?'tui-ptop-4':'']">
						<tui-icon :name="tabCur==index?item.icon+'-fill':item.icon" :color="tabCur==index?'#E41F19':'#666'" :size="item.size"></tui-icon>
					</view>
					<view class="tui-scale">{{item.text}}</view>
				</view>
			</block>
		</view>
		<!--tabbar-->
		
		<view class="tui-box">
			<tui-list-cell :arrow="true" :last="true" :radius="1" @click="goMyAddressIndex()">
				<view class="tui-address">
					<view v-if="(myAddressId > 0)">
						<view class="tui-userinfo">
							<text class="tui-name">{{myAddressInfo.name}}</text> {{myAddressInfo.phone}}
						</view>
						<view class="tui-addr">
							<view v-if="(myAddressInfo.is_home == 1)" class="tui-addr-tag">默认</view>
							<!-- <view class="tui-addr-tag">公司</view> -->
							<text>{{myAddressInfo.address_all}}</text>
						</view>
					</view>
					<view class="tui-none-addr" v-else>
						<image src="/static/images/index/map.png" class="tui-addr-img" mode="widthFix"></image>
						<text>选择收货地址</text>
					</view>
				</view>
				<view class="tui-bg-img"></view>
			</tui-list-cell>
			<view class="tui-top tui-goods-info">
				<tui-list-cell :hover="false" :lineLeft="0">
					<view class="tui-goods-title">商品信息</view>
				</tui-list-cell>
				<block v-for="(item,index) in goodsList" :key="index">
					<tui-list-cell :hover="false" padding="0">
						<view class="tui-goods-item">
							<image :src="item.goods_logo" class="tui-goods-img"></image>
							<view class="tui-goods-center">
								<view class="tui-goods-name">{{item.goods_name}}</view>
								<view class="tui-goods-attr">{{item.goods_format_name}}</view>
							</view>
							<view class="tui-price-right">
								<view>￥{{item.goods_money}}</view>
								<view>x{{item.goods_num}}</view>
							</view>
						</view>
					</tui-list-cell>
				</block>
				<tui-list-cell :hover="false">
					<view class="tui-padding tui-flex">
						<view>商品总额</view>
						<view>￥{{orderData.money_total}}</view>
					</view>
				</tui-list-cell>
				<tui-list-cell :hover="false">
					<view class="tui-padding tui-flex">
						<view>订单金额</view>
						<view>{{orderData.money_total}}</view>
					</view>
				</tui-list-cell>
				
				<tui-list-cell v-if="false" :hover="false">
					<view class="tui-padding tui-flex">
						<view>配送费</view>
						<view>￥0.00</view>
					</view>
				</tui-list-cell>
				
				<view class='tui-notice-board' v-if="hasNotice == 1">
					<view class="tui-icon-bg">
						<tui-icon name="news-fill" :size='24' color='#f54f46'></tui-icon>
					</view>
					<view class="tui-scorll-view">
						<view class="tui-notice" :class="[animation?'tui-animation':'']">{{noticeOrderPrepay}}</view>
					</view>
				</view>
				
				<tui-list-cell v-if="false" :hover="false" :lineLeft="0" padding="0">
					<view class="tui-remark-box tui-padding tui-flex">
						<view>订单备注</view>
						<input type="text" class="tui-remark" placeholder="选填: 请先和商家协商一致" placeholder-class="tui-phcolor"></input>
					</view>
				</tui-list-cell>
				
				<tui-list-cell :hover="false" :last="true">
					<view class="tui-padding tui-flex tui-total-flex">
						<view class="tui-flex-end tui-color-red">
							<view class="tui-black">支付合计： </view>
							<view class="tui-size-26">￥</view>
							<view class="tui-price-large">{{orderData.money_wx}}</view>
							<view class="tui-size-26">.00</view>
						</view>
					</view>
				</tui-list-cell>
				<tui-list-cell v-if="false" :hover="false" :last="true">
					<view class="tui-padding tui-flex tui-total-flex">
						<view class="tui-flex-end tui-color-red">
							<view class="tui-black">余额支付合计： </view>
							<view class="tui-size-26">￥</view>
							<view class="tui-price-large">{{orderData.money_wx}}</view>
							<view class="tui-size-26">.00</view>
						</view>
					</view>
				</tui-list-cell>
			</view>

			<view v-if="false" class="tui-top">
				<tui-list-cell :hover="myMoneyEnough" :radius="1">
					<view class="tui-flex">
						<view class="tui-balance">我的钱包余额</view>
						<view class="tui-pr-30 tui-light-dark">￥{{myMoney}}</view>
					</view>
				</tui-list-cell>
			</view>
			
		</view>
		<view class="tui-safe-area"></view>
		<view class="tui-tabbar">
			<view class="tui-flex-end tui-color-red tui-pr-20">
				<view class="tui-black">订单金额: </view>
				<view class="tui-size-26">￥</view>
				<view class="tui-price-large">{{orderData.money_total}}</view>
				<view class="tui-size-26">.00</view>
			</view>
			<view class="tui-pr25">
				<tui-button width="200rpx" height="70rpx" type="danger" shape="circle" @click="doPayOrder">确认支付</tui-button>
			</view>
		</view>
	</view>
</template>

<script>
	import footerMenu from "@/components/footer-menu.vue"
	import tuiIcon from "@/components/thorui/tui-icon/tui-icon.vue"
	import tuiButton from "@/components/thorui/tui-button/tui-button"
	import tuiListCell from "@/components/thorui/tui-list-cell/tui-list-cell"
	import tuiBottomPopup from "@/components/thorui/tui-bottom-popup/tui-bottom-popup"
	export default {
		components: {
			footerMenu,
			tuiIcon,
			tuiButton,
			tuiListCell,
			tuiBottomPopup
		},
		data() {
			return {
				prepayId: 0,
				myAddressId: 0,		//选择的地址
				myAddressInfo: [],
				myMoney: 0,			//我的钱包余额
				myMoneyGold: 0,		//我的消费积分余额
				myMoneyEnough: true,

				orderData: [],		//订单数据
				goodsList: [],		//商品数据
				
				hasCoupon: true,	//有优惠券
				insufficient: true,	//余额不足
				
				animation: false,
				hasNotice: 0,
				noticeOrderPrepay: '',
				
				//底部导航栏tabbar
				tabCur: 1,
				tabbar: [{icon: "home", text: "首页", size: 21}, 
					{icon: "people", text: "我的", size: 24}],
			}
		},
		methods: {
			
			//底部导航栏tabbar
			tabbarSwitch: function(e) {
				let index = e.currentTarget.dataset.index;
				this.tabCur = index;
				if (index == 0) {
					uni.switchTab({ url: '/pages/site/index' })
				} else {
					uni.switchTab({ url: '/pages/my/index' })
				}
			},

			goMyAddressIndex: function() {
				uni.redirectTo({
					url: "/pages/my-address/index?prepay_id=" + this.prepayId
				})
			},
			
			
			doPayOrder: function() {
				if(this.myAddressId == 0) {
					this.tui.toast("请选择需要配送的地址信息");
					return;
				}
				
				uni.request({
					url: this.$apiUrl+ '/api/my-order/prepay-address',
					header: {
						'userId': uni.getStorageSync('userId'),
						'userToken': uni.getStorageSync('userToken'),
            'shopId': uni.getStorageSync('shopId')
					},
					data: {
						prepay_id: this.prepayId,
						address_id: this.myAddressId,
					},
					success: (res) => {
						let resData = res.data['data'];
						let resErr = res.data['error'];
						if(resErr == 0) {	//信息无误去付款--跳转到付款台
							uni.reLaunch({
								url: "/pages/pay/wx-order?prepay_id=" + this.prepayId
							})
						}else{
							this.tui.toast("配送地址有误", 200);
						}
					}
				});
			},
			
			//得到我的预支付订单的详情
			getMyOrderPrepayInfo: function() {
				uni.request({
					url: this.$apiUrl + '/api/my-order/prepay-info',
					header: {
						'userId': uni.getStorageSync('userId'),
						'userToken': uni.getStorageSync('userToken'),
            'shopId': uni.getStorageSync('shopId')
					},
					data: {
						prepay_id: this.prepayId,
					},
					success: (res) => {
						let resData = res.data['data'];
						let resError = res.data['error'];
						if(resError == 0) {
						//	console.log(resData);
							this.orderData = resData;
							this.goodsList = resData['goods_list'];
						}else{
							this.tui.toast("数据请求失败~"); return;
						}
					}
				});
			},
			
			//我的优先地址
			getMyAddressBest: function(addressId) {
				uni.request({
					url: this.$apiUrl + '/api/my-address/best-pick',
					header: {
						'userId': uni.getStorageSync('userId'),
						'userToken': uni.getStorageSync('userToken'),
            'shopId': uni.getStorageSync('shopId')
					},
					data: {
						'address_id': addressId,
					},
					success: (res) => {
						let resData = res.data['data'];
						let resError = res.data['error'];
						if(resError == 0) {
							this.myAddressId = resData.id;
							this.myAddressInfo = resData;
						}else{
							this.tui.toast("数据请求失败~"); return;
						}
					}
				});
			},
			
			//我的个人信息
			getMyBaseInfo: function() {
				uni.request({
					url: this.$apiUrl+ '/api/user/baseInfo',
					header: {
						'userId': uni.getStorageSync('userId'),
						'userToken': uni.getStorageSync('userToken'),
            'shopId': uni.getStorageSync('shopId')
					},
					success: (res) => {
						let resData = res.data['data'];
						this.myMoney = resData.money;
						this.myMoneyGold = resData.gold;
					}
				});
			},
			
			getWxMarketSet: function(){
				uni.request({
					url: this.$apiUrl + '/api/site/wx-market-set',
					success: (res) => {
						let resData = res.data['data'];
						this.hasNotice = resData.has_notice_order_prepay;
						this.noticeOrderPrepay = resData.notice_order_prepay;
					}
				});
			},
			
			
		},
		
		onLoad: function(options) {
			this.prepayId = options.prepay_id || 0;
			this.myAddressId = options.address_id || 0;
			this.getMyBaseInfo();
			this.getWxMarketSet();
			this.getMyOrderPrepayInfo();
			this.getMyAddressBest(this.myAddressId);
			
			setTimeout(() => {
				this.animation = true
			}, 600);
		},
		
		onPullDownRefresh: function() {
			return;
		},
		
		onReachBottom: function() {
			return;
		},
	}
</script>

<style>
	
	/*tabbar*/
	.tui-tabbar {width: 100%; position: fixed; display: flex;align-items: center;justify-content: space-between;z-index: 99999;background: #fff; height: 100rpx;left: 0;bottom: 0;padding-bottom: env(safe-area-inset-bottom);}
	.tui-safearea-bottom {width: 100%;height: env(safe-area-inset-bottom);}
	.tui-tabbar::before {content: ''; width: 100%; border-top: 1rpx solid #d9d9d9;position: absolute;top: 0;left: 0;-webkit-transform: scaleY(0.5);transform: scaleY(0.5);}
	.tui-tabbar-item {flex: 1;width: 25%;display: flex;align-items: center;flex-direction: column;justify-content: space-between;font-size: 24rpx;color: #666;height: 80rpx;}
	.tui-ptop-4 {padding-top: 4rpx;}
	.tui-scale {font-weight: bold;transform: scale(0.8);transform-origin: center 100%;line-height: 30rpx;}
	.tui-item-active {color: #e41f19 !important;}
	/*tabbar*/
	
	
	.container {
		padding-bottom: 98rpx;
	}

	.tui-box {
		padding: 20rpx 0 118rpx;
		box-sizing: border-box;
	}

	.tui-address {
		min-height: 80rpx;
		padding: 10rpx 0;
		box-sizing: border-box;
		position: relative;
	}

	.tui-userinfo {
		font-size: 30rpx;
		font-weight: 500;
		line-height: 30rpx;
		padding-bottom: 12rpx;
	}

	.tui-name {
		padding-right: 40rpx;
	}

	.tui-addr {
		font-size: 24rpx;
		word-break: break-all;
		padding-right: 25rpx;
	}

	.tui-addr-tag {
		padding: 5rpx 8rpx;
		flex-shrink: 0;
		background: #EB0909;
		color: #fff;
		display: inline-flex;
		align-items: center;
		justify-content: center;
		font-size: 25rpx;
		line-height: 25rpx;
		transform: scale(0.8);
		transform-origin: 0 center;
		border-radius: 6rpx;
	}

	.tui-bg-img {
		position: absolute;
		width: 100%;
		height: 8rpx;
		left: 0;
		bottom: 0;
		background: url("") repeat;
	}

	.tui-top {
		margin-top: 20rpx;
		overflow: hidden;
	}

	.tui-goods-title {
		font-size: 28rpx;
		display: flex;
		align-items: center;
	}

	.tui-padding {
		box-sizing: border-box;
	}

	.tui-goods-item {
		width: 100%;
		padding: 20rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
	}

	.tui-goods-img {
		width: 180rpx;
		height: 180rpx;
		display: block;
		flex-shrink: 0;
	}

	.tui-goods-center {
		flex: 1;
		padding: 20rpx 8rpx;
		box-sizing: border-box;
	}

	.tui-goods-name {
		max-width: 310rpx;
		word-break: break-all;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		font-size: 26rpx;
		line-height: 32rpx;
	}

	.tui-goods-attr {
		font-size: 22rpx;
		color: #888888;
		line-height: 32rpx;
		padding-top: 20rpx;
		word-break: break-all;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
	}

	.tui-price-right {
		text-align: right;
		font-size: 24rpx;
		color: #888888;
		line-height: 30rpx;
		padding-top: 20rpx;
	}

	.tui-flex {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 26rpx;
	}
	.tui-total-flex{
		justify-content: flex-end;
	}

	.tui-color-red,
	.tui-invoice-text {
		color: #E41F19;
		padding-right: 30rpx;
	}

	.tui-balance {
		font-size: 28rpx;
		font-weight: 500;
	}

	.tui-black {
		color: #222;
		line-height: 30rpx;
	}

	.tui-gray {
		color: #888888;
		font-weight: 400;
	}

	.tui-light-dark {
		color: #666;
	}

	.tui-goods-price {
		display: flex;
		align-items: center;
		padding-top: 20rpx;
	}

	.tui-size-26 {
		font-size: 26rpx;
		line-height: 26rpx;
	}

	.tui-price-large {
		font-size: 34rpx;
		line-height: 32rpx;
		font-weight: 600;
	}

	.tui-flex-end {
		display: flex;
		align-items: flex-end;
		padding-right: 0;
	}

	.tui-phcolor {
		color: #B3B3B3;
		font-size: 26rpx;
	}

	/* #ifndef H5 */
	.tui-remark-box {
		padding: 22rpx 30rpx;
	}

	/* #endif */
	/* #ifdef H5 */
	.tui-remark-box {
		padding: 26rpx 30rpx;
	}

	/* #endif */

	.tui-remark {
		flex: 1;
		font-size: 26rpx;
		padding-left: 64rpx;
	}

	.tui-scale-small {
		transform: scale(0.8);
		transform-origin: 100% center;
	}

	.tui-scale-small .wx-switch-input {
		margin: 0 !important;
	}

	/* #ifdef H5 */
	>>>uni-switch .uni-switch-input {
		margin-right: 0 !important;
	}

	/* #endif */
	.tui-tabbar {
		width: 100%;
		height: 98rpx;
		background: #fff;
		position: fixed;
		left: 0;
		bottom: 0;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		font-size: 26rpx;
		box-shadow: 0 0 1px rgba(0, 0, 0, .3);
		padding-bottom: env(safe-area-inset-bottom);
		z-index: 999;
	}

	.tui-pr-30 {
		padding-right: 30rpx;
	}

	.tui-pr-20 {
		padding-right: 20rpx;
	}

	.tui-none-addr {
		height: 80rpx;
		padding-left: 5rpx;
		display: flex;
		align-items: center;
	}

	.tui-addr-img {
		width: 36rpx;
		height: 46rpx;
		display: block;
		margin-right: 15rpx;
	}


	.tui-pr25 {
		padding-right: 25rpx;
	}

	.tui-safe-area {
		height: 1rpx;
		padding-bottom: env(safe-area-inset-bottom);
	}
	
	
	
	.tui-notice-board {
		width: 100%;
		padding-right: 30upx;
		box-sizing: border-box;
		font-size: 28upx;
		height: 60upx;
		background: #fff8d5;
		display: flex;
		align-items: center;
	}
		
	.tui-icon-bg {
		background: #fff8d5;
		padding-left: 30upx;
		position: relative;
		z-index: 10;
	}
		
	.tui-icon-class {
		margin-right: 12upx;
	}
		
	.tui-scorll-view {
		flex: 1;
		line-height: 1;
		white-space: nowrap;
		overflow: hidden;
		color: #f54f46;
	}
		
	.tui-notice {
		transform: translateX(100%);
	}
		
	.tui-animation {
		-webkit-animation: tui-rolling 12s linear infinite;
		animation: tui-rolling 12s linear infinite;
	}
		
	@-webkit-keyframes tui-rolling {
		0% {
			transform: translateX(100%);
		}
		
		100% {
			transform: translateX(-170%);
		}
	}
		
	@keyframes tui-rolling {
		0% {
			transform: translateX(100%);
		}
		
		100% {
			transform: translateX(-170%);
		}
	}
</style>